package com.ruoyi.process.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.process.domain.ProcessOrder;
import com.ruoyi.process.service.IProcessOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产订单Controller
 * 
 * @author chao
 * @date 2024-07-25
 */
@Controller
@RequestMapping("/process/order")
public class ProcessOrderController extends BaseController
{
    private String prefix = "process/order";

    @Autowired
    private IProcessOrderService processOrderService;

    @Autowired
    private IMainUnitesService mainUnitesService;

    @RequiresPermissions("process:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询生产订单列表
     */
    @RequiresPermissions("process:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessOrder processOrder)
    {
        startPage();
        List<ProcessOrder> list = processOrderService.selectProcessOrderList(processOrder);
        return getDataTable(list);
    }

    /**
     * 导出生产订单列表
     */
    @RequiresPermissions("process:order:export")
    @Log(title = "生产订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessOrder processOrder)
    {
        List<ProcessOrder> list = processOrderService.selectProcessOrderList(processOrder);
        ExcelUtil<ProcessOrder> util = new ExcelUtil<ProcessOrder>(ProcessOrder.class);
        return util.exportExcel(list, "生产订单数据");
    }

    /**
     * 新增生产订单
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/add";
    }

    /**
     * 新增保存生产订单
     */
    @RequiresPermissions("process:order:add")
    @Log(title = "生产订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessOrder processOrder)
    {
        return toAjax(processOrderService.insertProcessOrder(processOrder));
    }

    /**
     * 修改生产订单
     */
    @RequiresPermissions("process:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ProcessOrder processOrder = processOrderService.selectProcessOrderById(id);
        mmap.put("processOrder", processOrder);
        mmap.put("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/edit";
    }

    /**
     * 修改保存生产订单
     */
    @RequiresPermissions("process:order:edit")
    @Log(title = "生产订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessOrder processOrder, Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return toAjax(processOrderService.updateProcessOrder(processOrder));

    }

    /**
     * 删除生产订单
     */
    @RequiresPermissions("process:order:remove")
    @Log(title = "生产订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(processOrderService.deleteProcessOrderByIds(ids));
    }



    @RequiresPermissions("process:order:remove")
    @Log(title = "销售出库", businessType = BusinessType.OTHER)
    @PostMapping( "/excuteOutStorge")
    @ResponseBody
    public AjaxResult excuteOutStorge(@RequestBody ProcessOrder processOrder)
    {
        return toAjax(processOrderService.excuteOutStorge(processOrder));
    }
}
